<?php

session_start();
require_once("../include/MySugarApplication.php");
require_once("../include/CampaignSoap.php");
$app->checkSession('Campaigns/Data.php');

$query='';
$quote = '"';
$deletedQuery = '';
$deleteCount = 0;
$startIndex = 0;
$numberOfRows = 0;
$includeDeleted = false;
$orderby = ' campaigns.date_modified desc';
$recordCountOnly=$_REQUEST["recordCount"];
$keysOnly=$_REQUEST["keysOnly"];
$account_id = $_REQUEST["account_id"];

if ($account_id != "") {

	$query = " accounts.id=".$quote.$account_id.$quote;

} else {
	
	$startIndexStr = $_REQUEST["startIndex"];
	if ($startIndexStr == "")
		$startIndex = 0;
	else
		$startIndex = (int)$startIndexStr;

	$numberOfRowsStr = $_REQUEST["numberOfRows"];
	if ($numberOfRowsStr == "")
		$numberOfRows = 0;
	else
		$numberOfRows = (int)$numberOfRowsStr;

	if ( $recordCountOnly != "Y" ) {

		if ($_REQUEST["lastModifiedOn"] != "" ) {
			$query = " campaigns.date_modified >= ";
			$query = $query.$quote.$_REQUEST["lastModifiedOn"].$quote;
	
			$includeDeleted = true;
		}
	}
}

$rows= $campaignSoap->getRows($query, $startIndex, $numberOfRows, $orderby, $includeDeleted);
$recordCount = $campaignSoap->getTotalRows();


?>
<!DOCTYPE HTML SYSTEM "http://www.bitzermobile.com/bitzer/dtd/HTML401-loose-bitzer.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="bitzer-component" content="data">
		<?php
		if (!$isBitzer) {
        	echo '
			<title>MySugar Campaigns</title>
			<LINK REL=StyleSheet HREF="',$app->appRoot,'include/style.css" TYPE="text/css" MEDIA=screen>
        	<script type="text/javascript" src="',$app->appRoot,'include/niftycube.js"></script>
        	<script type="text/javascript">
            window.onload=function(){
				var width = "1600px";
				setAppRoot("',$app->appRoot,'");
                Nifty("div#container");
                Nifty("div#content,div#nav","same-height small");
                Nifty("div#header,div#footer","small");
                Nifty("ul.postnav a","transparent");
				setWidth("div#container", width);
				setWidth("div#content", width);
				setWidth("div#footer", width);
            }
        </script>';
        } else {
			echo '<title></title>';
		}
		?>    
    </head>
    <body>
    <?php
	if (!$isBitzer) {
		echo '
        <div id="container">
            <div id="header">
            	<table width="100%">
					<tr>
						<td width="95%"><h1>Campaigns</h1></td>
						<td align="right" width="5%"><a href="',$app->appRoot,'Logout.php">Logout</a></td>
					</tr>
				</table>
            </div>
            <div id="content">
            <br>';
	}
	?>
                <table data-bitzer-data="true" data-bitzer-title="Campaigns"
                       data-bitzer-desc="List of campaigns"
                       data-bitzer-component="Campaigns"
					   data-bitzer-record-count="<?php echo $recordCount; ?>"
                       <?php if (!$isBitzzer) echo 'border="0" cellspacing="0" cellpadding="0"'; ?>
                       <?php if ($numberOfRows > 0) {
					   		echo 'data-bitzer-start-index="',$startIndex,'" data-bitzer-number-of-rows="',$numberOfRows,'" ';
							}
						?>
                       class="list"  width="100%">
					   
<?php
if( $recordCount > 0 && $recordCountOnly != "Y"){  
?>
						<tr>
						<?php
						if ($keyOnly != "Y" && !$isBitzer) {
							if ($array['deleted'] == "1") {
								echo '<th></th>';
							} else {
                        		echo '<th>Delete</th>';
							}
                        }
						?>
                        <th data-bitzer-field="id" data-bitzer-key-order="1" data-bitzer-ignore-anchor="true">id</th>
                        <?php
						if ($keysOnly != "Y" ) {
						?>
                        	<th data-bitzer-field="name">name</th>
                        	<th data-bitzer-field="date_entered">date_entered</th>
                        	<th data-bitzer-field="date_modified">date_modified</th>
                            <th data-bitzer-field="modified_user_id">modified_user_id</th>
                            <th data-bitzer-field="created_by">created_by</th>
                            <th data-bitzer-field="assigned_user_id">assigned_user_id</th>
                            <th data-bitzer-field="tracker_key">tracker_key</th>
                            <th data-bitzer-field="tracker_count">tracker_count</th>
                            <th data-bitzer-field="refer_url">refer_url</th>
                            <th data-bitzer-field="tracker_text">tracker_text</th>
                            <th data-bitzer-field="start_date">start_date</th>
                            <th data-bitzer-field="end_date">end_date</th>
                            <th data-bitzer-field="status">status</th>
                            <th data-bitzer-field="impressions">impressions</th>
                            <th data-bitzer-field="currency_id">currency_id</th>
                            <th data-bitzer-field="budget">budget</th>
                            <th data-bitzer-field="expected_cost">expected_cost</th>
                            <th data-bitzer-field="actual_cost">actual_cost</th>
                            <th data-bitzer-field="expected_revenue">expected_revenue</th>
                            <th data-bitzer-field="campaign_type">campaign_type</th>
                            <th data-bitzer-field="objective">objective</th>
                            <th data-bitzer-field="content">content</th>
                            <th data-bitzer-field="frequency">frequency</th>
                        <?php
						}
						?>
                        </tr>
                        <?php
						foreach($rows as $record){
							$array= $app->nameValuePairToSimpleArray($record['name_value_list']);
                        	echo '<tr';
							if ($array['deleted'] == "1") {
								if (!$isBitzer) {
									$fontStartTag = '<font color="#FF0000"><del>';
									$fontEndTag = '</del></font>';
								}
								echo ' data-bitzer-deleted="true"';
							}
							echo '>';
									
							if ($keyOnly != "Y" && !$isBitzer) {
								if ($array['deleted'] == "1") {
									echo '<td></td>';
								} else {
									echo '<td><a href="Save.php?actionType=DELETE&id=', $array['id'],'">Delete</a></td>';
								}
							}
								
							echo '<td>',$fontStartTag;	
							if ($keyOnly != "Y" && !$isBitzer) {
								echo '<a href="Form.php?actionType=UPDATE&id=', $array['id'],'">',$array['id'],'</a>';
							} else {
								echo $array['id'];
							}
							echo $fontEndTag,'</td>';
								
							if ($keysOnly != "Y") {
									echo '<td>',$fontStartTag,$array['name'],$fontEndTag,'</td>';
                                	echo '<td>',$fontStartTag,$array['date_entered'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['date_modified'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['modified_user_id'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['created_by'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['assigned_user_id'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['tracker_key'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['tracker_count'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['refer_url'],$fontEndTag,'</td>';
                                	echo '<td>',$fontStartTag,$array['tracker_text'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['start_date'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['end_date'],$fontEndTag,'</td>';
                                	echo '<td>',$fontStartTag,$array['status'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['impressions'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['currency_id'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['budget'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['expected_cost'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['actual_cost'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['expected_revenue'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['campaign_type'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['objective'],$fontEndTag,'</td>';
									echo '<td>',$fontStartTag,$array['content'],$fontEndTag,'</td>';
                                	echo '<td>',$fontStartTag,$array['frequency'],$fontEndTag,'</td>';
							}
                            echo '</tr>';
						}
						
						
						
						
						
}
						?>
                </table>
    <?php
	if (!$isBitzer) {
		echo '
                <br>
                <ul class="postnavList">
                    <li><a href="Form.php">Add New Campaign</a></li>
                </ul>
            </div>
            <div id="footer"></div>
        </div>';
	}
	?>
    </body>
</html>
